package Control;

import Database.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class AddCustomer extends JFrame implements ActionListener
{
	private JTextField jtName, jtPhone, jtAddress, jtEmail;
	private JLabel logoPicture;
	private JButton add, cancel;
	private JPanel logo, p1, p2;
	private TableOperations to;
	private ImageIcon icon = new ImageIcon("frameIcon.png");

	public AddCustomer()
	{
		setIconImage(icon.getImage());
		setTitle("MDL - Add Loyalty Customer");
		setSize(450, 400);
		setLocationRelativeTo(null);
		setResizable(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLayout(new GridLayout(3, 1));

		GridBagLayout gridbagLayoutSettings = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();
		Insets insets = new Insets(5, 5, 5, 5);
		to = new TableOperations();

		jtName = new JTextField(25);
		jtPhone = new JTextField(25);
		jtAddress = new JTextField(25);
		jtEmail = new JTextField(25);

		BufferedImage myPicture = null;
		try
		{
			myPicture = ImageIO.read(new File("logo.png"));
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		logo = new JPanel();
		logoPicture = new JLabel(new ImageIcon(myPicture));
		logo.add(logoPicture);
		this.add(logo);

		// Panel 1
		p1 = new JPanel();
		p1.setLayout(gridbagLayoutSettings);

		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Name"), gc);

		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(jtName, gc);

		gc.gridx = 0;
		gc.gridy = 3;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Phone"), gc);

		gc.gridx = 1;
		gc.gridy = 3;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(jtPhone, gc);

		gc.gridx = 0;
		gc.gridy = 5;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Address"), gc);

		gc.gridx = 1;
		gc.gridy = 5;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(jtAddress, gc);

		gc.gridx = 0;
		gc.gridy = 7;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(new JLabel("Email"), gc);

		gc.gridx = 1;
		gc.gridy = 7;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		p1.add(jtEmail, gc);

		this.add(p1, BorderLayout.CENTER);

		// Panel 2
		p2 = new JPanel();
		p2.setLayout(gridbagLayoutSettings);

		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add = new JButton("Add Customer");
		add.addActionListener(this);
		p2.add(add, gc);

		gc.gridx = 3;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		p2.add(cancel, gc);

		this.add(p2, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == cancel)
		{
			this.dispose();
		} else if (e.getSource() == add)
		{
			if (jtName.getText().equals("") || jtAddress.getText().equals("")
					|| jtPhone.getText().equals("") || jtEmail.getText().equals(""))
				JOptionPane.showMessageDialog(null, "Please fill in all fields");			

			else
			{
				boolean valid;
				valid = validateEmailAddress();

				if (!valid)
					JOptionPane.showMessageDialog(null,	"Error Invalid E-mail address !");
				else
				{
					LoyaltyCustomer c = new LoyaltyCustomer(jtName.getText(), jtAddress.getText(), jtPhone.getText(), jtEmail.getText());
					to.addCustomer(c);
					JOptionPane.showMessageDialog(null, "Customer added.");
					jtName.setText("");
					jtAddress.setText("");
					jtPhone.setText("");
					jtEmail.setText("");
				}
			}
		}
	}

	// Vaildate Email
	public boolean validateEmailAddress()
	{
		int count = 0;
		int start = 0;

		for (int i = 0; i < jtEmail.getText().length(); i++)
		{
			i = jtEmail.getText().indexOf("@", start);
			if (i != -1)
			{
				count++;
				start = i + 1;
			} else
			{
				break;
			}
		}
		return (count == 1);

	}
	public static void main(String[] args)
	{
		AddCustomer ac = new AddCustomer();
		ac.setVisible(true);
	}
}
